<?php

namespace App\Http\Controllers\Admin;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Http\Requests\DepartmentRequest;
use App\Models\Department;
use Illuminate\Http\Request;

class DepartmentController extends Controller
{
    use ResponseData;
    public function list(Department $model,Request $request){
        $category = $request->input('category',0);
        $size = $request->input('size',10);
        if($request->filled('level')){
            $model = $model->where('level',$request->level);
        }
        if($request->filled('name')){
            $model = $model->where('name','like','%'.$request->name.'%');
        }
        if($request->filled('pid')){
            $model = $model->where('pid','like','%'.$request->pid.'%');
        }
        if($request->filled('status')&&$request->status=='all'){
            return $this->success($model->get()) ;
        }else{
            $model = $model->paginate($size);
            $data = $this->getGradeList($model);
            return $this->success($data);
        }
    }
    protected function getGradeList($data){
        foreach ($data as $k=>$v){
            $arr = explode(',',$v->pid);
            $array = [];
            foreach ($arr as $m=>$n){
                $res = $this->getData($n);
                if($res){
                    $array[$m] = $res;
                }
            }
            $data[$k]['pid_list'] = $array;
        }
        return $data;
    }
    protected function getData($id){
        return Department::query()->find($id);
    }
    public function store(Department $model,DepartmentRequest $request){
        $data = $request->all();
        $model->fill($data);
        $model->save();
        return $this->success('保存成功');
    }
    public function update(Department $model,Request $request){
        $data = $model->find($request->id);
        if($request->filled('status')&&$request->status==2){
            //直属下级终止
            if($data->level==3){
                Department::query()->where('pid',$request->id)->update(['status'=>2]);
            }elseif($data->level==2){
                Department::query()->where('pid',$data->pid.','.$request->id)->update(['status'=>2]);
            }elseif($data->level==1){
                //停用2级
                Department::query()->where('level',2)->where('pid',$data->id)->update(['status'=>2]);
                $two = Department::query()->where('level',2)->where('pid',$data->id)->pluck('id');
                foreach ($two as $v){
                    //停用三级
                    Department::query()->where('level',3)
                        ->where('pid',$request->id.','.$v)
                        ->update(['status'=>2]);
                }

            }

        }
        $res = $request->all();
        $data->update($res);

        return $this->success('修改成功');
    }
    //编辑
    public function edit(Department $model,Request $request){
        $data = $model->find($request->id);
        return $this->success($data);
    }
}
